﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Gantt
{
    [MetadataType(typeof(TaskMetadata))]
    public partial class Task
    {

        /// <summary>
        /// Parent node identifier.
        /// Used for more type flexible parent identifier processing since parentId provided
        /// from the client side may contain string value.
        /// So this property tries to convert string value to integer and sets it to `null` in case of failed attempt.
        /// </summary>
        [NotMapped]
        public virtual string parentId
        {
            set
            {
                // for root level nodes we keep `null`
                if (String.IsNullOrEmpty(value) || value.Equals("root", StringComparison.InvariantCultureIgnoreCase))
                {
                    parentIdRaw = null;
                    return;
                }

                try
                {
                    parentIdRaw = Convert.ToInt32(value);
                }
                catch (System.Exception)
                {
                    parentIdRaw = null;
                }
            }

            get
            {
                return !parentIdRaw.HasValue ? null : Convert.ToString(parentIdRaw.Value);
            }
        }

        /// <summary>
        /// Phantom parent identifier.
        /// </summary>
        [NotMapped]
        public virtual string PhantomParentId { get; set; }

        /// <summary>
        /// Used by `Newtonsoft.JSON` to exclude phantom parent identifier during entity serialization.
        /// </summary>
        /// <returns>false</returns>
        public bool ShouldSerializePhantomParentId() { return false; }

        [JsonIgnore]
        [NotMapped]
        public override String PhantomIdField { get { return "PhantomId"; } }

        [JsonProperty("PhantomId")]
        [NotMapped]
        public override string PhantomId { get; set; }


        [JsonIgnore]
        [NotMapped]
        public string PhantomCalendarId { get; set; }

        [NotMapped]
        public string CalendarId
        {
            set
            {
                PhantomCalendarId = value;
                if (value == null) return;

                try
                {
                    CalendarIdRaw = Convert.ToInt32(value);
                }
                catch (System.Exception)
                {
                    CalendarIdRaw = null;
                }
            }

            get
            {
                return CalendarIdRaw > 0 ? Convert.ToString(CalendarIdRaw) : PhantomCalendarId;
            }
        }

        [NotMapped]
        public virtual bool leaf { get { return ChildrenRaw.Count == 0; } }

        [NotMapped]
        public virtual ICollection<Task> children
        {
            get { if (!leaf) return ChildrenRaw; return null; }
            set { ChildrenRaw = value; }
        }
    }


    public class TaskMetadata : NodeMetadata<Task>
    {
        [JsonIgnore]
        public Nullable<int> CalendarIdRaw { get; set; }
        [JsonIgnore]
        public virtual ICollection<Task> ChildrenRaw { get; set; }
        [JsonIgnore]
        public virtual ICollection<Assignment> Assignments { get; set; }
        [JsonIgnore]
        public virtual Calendar Calendar { get; set; }
        [JsonIgnore]
        public virtual ICollection<Dependency> Predecessors { get; set; }
        [JsonIgnore]
        public virtual ICollection<Dependency> Successors { get; set; }
    }


}
